#pragma once

#include <fstream>
#include <string>
#include <iostream>
#include <unordered_map>

const static std::string sep = ": ";

class Dict
{
private:
    void LoadDict(const std::string &dict_path)
    {
        std::fstream in(dict_path);
        if(!in.is_open())
        {
            std::cout << "open fail!" << std::endl;
            exit(1);
        }

        std::string line;
        while(std::getline(in,line))
        {
            if(line.empty()) continue;

            auto pos = line.find(sep);
            if(pos == std::string::npos) continue;

            std::string key = line.substr(0,pos);
            std::string value = line.substr(pos + sep.size());
            if(key.empty() || value.empty()) continue;

            // _dict.insert(std::make_pair(key,value));
            _dict[key] = value; // 直接赋值，不需要 make_pair
        }
        in.close();
    }

public:
    Dict(const std::string &dict_path)
        : _dict_path(dict_path)
    {
        LoadDict(_dict_path);
    }

    const std::string Translate(std::string word)
    {
        if(word.empty()) return "Not exist";
        auto iter = _dict.find(word);
        if(iter == _dict.end()) return "Not exist!";
        else return iter->second;
    }
    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _dict_path;
};
